*************************************************************************************************************
				                      	** Evolution of tariff rates  **
*************************************************************************************************************  

use export_merged.dta, clear 
  
keep ym year cmcode cty_id exp_usd bg_q 
*keep if ym>=tm(2017m1) & ym<=tm(2019m6)  
drop if cty_id==142 // drop "exports" to China
collapse (sum) exp_usd bg_q (mean) year, by(ym cmcode cty_id)

  
* Merge with 中美贸易战商品代码清单        
**合并10位
merge m:1 cmcode using "trade_war_product_list_hs10.dta", update
drop if _merge==2 
drop 序号 中国海关税则号 中国海关商品名称 length_cmcode cmcode10 _merge 

gen cmcode8=substr(cmcode,1,8)
**合并8位
merge m:1 cmcode8 using "trade_war_product_list_hs8_supplement.dta",update 
drop if _merge==2 
drop cmcode8-_merge

gen cmcode6=substr(cmcode,1,6)
**合并6位
merge m:1 cmcode6 using "trade_war_product_list_hs6_supplement.dta",update
drop if _merge==2 
drop _merge 

* 34 billion round: annouced on June 15, 2018, enforced on July 6, 2018, tariff rate + 25% 
* 16 billion round: annouced on Aug 7, 2018, enforced on Aug 23, 2018, tariff rate + 25%
* 200 billion round: annouced on July 10, 2018, enforced on Sep 18, 2018, tariff rate + 10%

gen tariff_round=0
replace tariff_round=34 if 加征关税批次=="340亿商品"
replace tariff_round=16 if 加征关税批次=="160亿商品"
replace tariff_round=200 if 加征关税批次=="2000亿商品"	

drop if cmcode==" "
drop 加征关税批次

* Merge UN Comtrade 6-digit tariff rates
merge m:1 cmcode6 cty_id year using import_tariff_6digit_fill.dta
drop if _merge==2
drop _merge bg_q
replace tariff_rate=0 if tariff_rate==. // set the very few unmatched missing values of tariff rates as zero
  
* Adjust tariff rate for exports to the US using the tariff rate changes during trade war
replace tariff_rate=tariff_rate+25 if cty_id==502 & tariff_round==34 & ym>=tm(2018m7)  
replace tariff_rate=tariff_rate+25 if cty_id==502 & tariff_round==16 & ym>=tm(2018m8) 
replace tariff_rate=tariff_rate+10 if cty_id==502 & tariff_round==200 & ym>=tm(2018m9)  

* Figure 1*
preserve
keep if ym<=tm(2019m4)
gen US=(cty_id==502)
collapse (mean) tariff_rate [weight=exp_usd], by(ym US)
twoway (connect tariff_rate ym if US==0, msymbol(Th)) (connect tariff_rate ym if US!=0, lpattern(dash)), /// 
graphregion(color(white))  xtitle("") ytitle("average tariff rate") ///
legend(label(1 non-US) label(2 US)) xline(702, lpattern(dash)) 
graph export Fig-Tariff_evolution.eps, replace   
restore
 

*************************************************************************************************************
										** Commodity-Country level analysis **
*************************************************************************************************************  

use export_merged.dta, clear 
  
keep ym year cmcode cty_id exp_usd bg_q 
keep if ym>=tm(2017m1) & ym<=tm(2019m4)  
drop if cty_id==142 // drop "exports" to China
collapse (sum) exp_usd bg_q (mean) year, by(ym cmcode cty_id)

  
* Merge with 中美贸易战商品代码清单 (Following Zhikuo's work)      
**合并10位
merge m:1 cmcode using "trade_war_product_list_hs10.dta", update
drop if _merge==2 
drop 序号 中国海关税则号 中国海关商品名称 length_cmcode cmcode10 _merge 

gen cmcode8=substr(cmcode,1,8)
**合并8位
merge m:1 cmcode8 using "trade_war_product_list_hs8_supplement.dta",update 
drop if _merge==2 
drop cmcode8-_merge

gen cmcode6=substr(cmcode,1,6)
**合并6位
merge m:1 cmcode6 using "trade_war_product_list_hs6_supplement.dta",update
drop if _merge==2 
drop _merge 

* 34 billion round: annouced on June 15, 2018, enforced on July 6, 2018, tariff rate + 25% 
* 16 billion round: annouced on Aug 7, 2018, enforced on Aug 23, 2018, tariff rate + 25%
* 200 billion round: annouced on July 10, 2018, enforced on Sep 18, 2018, tariff rate + 10%

gen tariff_round=0
replace tariff_round=34 if 加征关税批次=="340亿商品"
replace tariff_round=16 if 加征关税批次=="160亿商品"
replace tariff_round=200 if 加征关税批次=="2000亿商品"	

drop if cmcode==" "
drop 加征关税批次
  
egen gt = group(cmcode ym) // product-time
egen ct = group(cty_id ym) // country-time
egen cg = group(cty_id cmcode)
egen g  = group(cmcode)

* generate HS-8 variable
gen cmcode8=substr(cmcode,1,8)
egen g8=group(cmcode8)
drop cmcode8

gen lnexport=ln(exp_usd)
gen ln_p=ln(exp_usd/bg_q)
gen ln_q=ln(bg_q)

* Merge UN Comtrade 6-digit tariff rates
merge m:1 cmcode6 cty_id year using import_tariff_6digit_fill.dta
drop if _merge==2
drop _merge bg_q
replace tariff_rate=0 if tariff_rate==. // set the very few unmatched missing values of tariff rates as zero
  
* Adjust tariff rate for exports to the US using the tariff rate changes during trade war
replace tariff_rate=tariff_rate+25 if cty_id==502 & tariff_round==34 & ym>=tm(2018m7)  
replace tariff_rate=tariff_rate+25 if cty_id==502 & tariff_round==16 & ym>=tm(2018m8) 
replace tariff_rate=tariff_rate+10 if cty_id==502 & tariff_round==200 & ym>=tm(2018m9) 
*replace tariff_rate=tariff_rate/100 // change tariff rate from percentage point to percent
 
/* Event-study analysis */
* generate event time for each group
gen event_time=.
replace event_time=ym-tm(2018m7) if tariff_round==34 & cty_id==502
replace event_time=ym-tm(2018m8) if tariff_round==16 & cty_id==502
replace event_time=ym-tm(2018m9) if tariff_round==200 & cty_id==502  
replace event_time=-7 if event_time<=-7
replace event_time=7  if event_time>=7 & event_time!=.

foreach num of numlist 7(-1)1{
	gen treat_pre_`num'=0
	replace treat_pre_`num'=1 if event_time*(-1)==`num'  
}
foreach num of numlist 0(1)7{
	gen treat_post_`num'=0
	replace treat_post_`num'=1 if event_time==`num'  
} 

 
* Figure 2 * 
preserve 
* set omitted group
replace treat_pre_1=0
 
qui reghdfe lnexp treat_pre_* treat_post_*, a(gt ct cg) cluster(g8)
estimates store event  
coefplot event, omitted keep(treat_pre_* treat_post_*) rename(treat_pre_7="<-6" treat_pre_*="- " treat_post_7=">6" treat_post_*=" " ) ///
vertical graphregion(color(white)) yline(0, lpattern(dash)) ///
ytitle(ln(exports)) xtitle(month after tariff increase) ylabel(-0.2(0.1)0.1)
graph export Fig-EventStudy-cgtlevel.eps, replace
  
qui reghdfe ln_p treat_pre_* treat_post_*, a(gt ct cg) cluster(g8)
estimates store event    
coefplot event, omitted keep(treat_pre_* treat_post_*) rename(treat_pre_7="<-6" treat_pre_*="- " treat_post_7=">6" treat_post_*=" " ) ///
vertical graphregion(color(white)) yline(0, lpattern(dash)) ///
ytitle(ln(export price/unit)) xtitle(month after tariff increase) ylabel(-0.2(0.1)0.1) 
graph export Fig-EventStudy-cg_price.eps, replace   
  
qui reghdfe ln_q treat_pre_* treat_post_*, a(gt ct cg) cluster(g8)
estimates store event    
coefplot event, omitted keep(treat_pre_* treat_post_*) rename(treat_pre_7="<-6" treat_pre_*="- " treat_post_7=">6" treat_post_*=" " ) ///
vertical graphregion(color(white)) yline(0, lpattern(dash)) ///
ytitle(ln(export quantity)) xtitle(month after tariff increase) ylabel(-0.2(0.1)0.1)
graph export Fig-EventStudy-cg_q.eps, replace  
restore 
 
 
gen lnTariff=ln(1+tariff_rate/100)
  
* Quantitative analysis of tariff rate and exports
* Table 3 *
* Elasticities 
qui reghdfe lnexport lnTariff, a(gt ct cg) cluster(g8)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) nocons onecol label replace 
qui reghdfe ln_p lnTariff, a(gt ct cg) cluster(g8)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) nocons onecol label append 
qui reghdfe ln_q lnTariff, a(gt ct cg) cluster(g8)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) nocons onecol label append  
   
* Table A1 *  
* Estimate a specification similar to Table IV of Fajgelbaum et al. QJE
qui reghdfe lnexport lnTariff , a(gt ct cg) cluster(g)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(3) nocons onecol label replace 
  
qui reghdfe lnexport lnTariff if cty_id==502 , a(ym g) // focus on export to US
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(3) nocons onecol label append 
  
* Run a one-month first-differencing specification (same as Fajgelbaum et al. (2019) Table IV Column 1
preserve
xtset cg ym
gen d_lnexport=d.lnexport 
gen d_lnTariff=d.lnTariff
gen HS4=substr(cmcode, 1, 4)
egen cs = group(cty_id HS4)

qui reghdfe d_lnexport d_lnTariff , a(gt ct cg) cluster(g)
outreg2 using tariff.xls, keep(d_lnTariff) adjr2 bdec(3) nocons onecol label append 
 
gen post1_3=(ym>=tm(2018m7) & ym<=tm(2018m9))
gen post4_6=(ym>=tm(2018m10) & ym<=tm(2018m12))
gen post7_9=(ym>=tm(2019m1) & ym<=tm(2019m3))
gen post10_12=(ym>=tm(2019m4) & ym<=tm(2019m6))
gen d_lnTariff_post1_3=d_lnTariff*post1_3
gen d_lnTariff_post4_6=d_lnTariff*post4_6
gen d_lnTariff_post7_9=d_lnTariff*post7_9
gen d_lnTariff_post10_12=d_lnTariff*post10_12

qui reghdfe d_lnexport d_lnTariff , a(gt ct cs) cluster(g) // Fajgelbaum et al. (2019)  control for country-sector rather than country-product FE
outreg2 using tariff.xls, keep(d_lnTariff) adjr2 bdec(3) nocons onecol label append  
restore
 
/* Impact of U.S. tariff rates on prices of Chinese exports to different markets. */

* Divide exporting destinations as 5 groups: (0) US (1) ASEAN (2) EU (3) Japan and Korea (4) Hong Kong, Taiwan, Macau (5) ROW
gen exp_g=5
replace exp_g=0 if cty_id==502
* Singapore 132, Brunei 105, Malaysia 122, Thailand 136, Phillippines 129, Indonesia 112, Vietnam 141, Laos 119, Myanmar 106, and Cambodia 107
replace exp_g=1 if cty_id==132 | cty_id==105 | cty_id==122 | cty_id==136 | cty_id==129 | cty_id==112 | cty_id==141 | cty_id==119 | cty_id==106 | cty_id==107
replace exp_g=2 if cty_id==108 | (cty_id>=301 & cty_id<=312) | cty_id==315 | cty_id==316 | cty_id==318 | cty_id==321 | cty_id==324 | cty_id==327 | cty_id==328 | cty_id==330 | (cty_id>=334 & cty_id<=336) | (cty_id>=350 & cty_id<=353)
replace exp_g=3 if cty_id==116 | cty_id==133
replace exp_g=4 if cty_id==110 | cty_id==121 | cty_id==143 
  
* generate the US tariff rate for each commodity in each month 
preserve
use import_tariff_6digit_fill.dta, clear 
keep if cty_id==502
ren tariff_rate tariff_rate_US
save import_tariff_6digit_fill_US.dta, replace
restore  

merge m:1 cmcode6 year using import_tariff_6digit_fill_US.dta
drop if _merge==2
drop _merge  
replace tariff_rate_US=0 if tariff_rate_US==. // set the very few unmatched missing values of tariff rates as zero

* Adjust tariff rate for exports to the US using the tariff rate changes during trade war
replace tariff_rate_US=tariff_rate_US+25 if tariff_round==34 & ym>=tm(2018m7)  
replace tariff_rate_US=tariff_rate_US+25 if tariff_round==16 & ym>=tm(2018m8)  
replace tariff_rate_US=tariff_rate_US+10 if tariff_round==200 & ym>=tm(2018m9) 
 
gen tariff_rate_US_g0=tariff_rate_US*(exp_g==0)
gen tariff_rate_US_g1=tariff_rate_US*(exp_g==1)
gen tariff_rate_US_g2=tariff_rate_US*(exp_g==2)
gen tariff_rate_US_g3=tariff_rate_US*(exp_g==3)
gen tariff_rate_US_g4=tariff_rate_US*(exp_g==4)
gen tariff_rate_US_g5=tariff_rate_US*(exp_g==5) 

gen lnTariff_US=ln(1+tariff_rate_US/100)
gen lnTariff_US_g0=lnTariff_US*(exp_g==0)
gen lnTariff_US_g1=lnTariff_US*(exp_g==1)
gen lnTariff_US_g2=lnTariff_US*(exp_g==2)
gen lnTariff_US_g3=lnTariff_US*(exp_g==3)
gen lnTariff_US_g4=lnTariff_US*(exp_g==4)
gen lnTariff_US_g5=lnTariff_US*(exp_g==5)
 
* Regression: response of exports to different destinations
* Table A3 *
qui reghdfe ln_p lnTariff, a(gt ct cg) cluster(g)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) nocons onecol replace 

qui reghdfe ln_p lnTariff lnTariff_US, a(ct cg) cluster(g)
outreg2 using tariff.xls, keep(lnTariff lnTariff_US) adjr2 bdec(4) nocons onecol append 
 
qui reghdfe ln_p lnTariff lnTariff_US_g*, a(ct cg) cluster(g)
outreg2 using tariff.xls, keep(lnTariff lnTariff_US_g*) adjr2 bdec(4) nocons onecol append 
  
   
  
*************************************************************************************************************
									** Firm-Commodity-Country-level analysis **
* Caution: Running regressions in this part will take a very long time (3.5-4h), be careful when trying to run it 
*************************************************************************************************************  

use export_merged.dta, clear 
  
keep firm ym year cmcode cty_id exp_usd bg_q 
keep if ym>=tm(2017m1) & ym<=tm(2019m4)  
drop if cty_id==142 // drop "exports" to China
 
collapse (sum) exp_usd bg_q (mean) year, by(firm cmcode cty_id ym) 
 
* Merge with 中美贸易战商品代码清单 (Following Zhikuo's work)      
**合并10位
merge m:1 cmcode using "trade_war_product_list_hs10.dta", update
drop if _merge==2 
drop 序号 中国海关税则号 中国海关商品名称 length_cmcode cmcode10 _merge 

gen cmcode8=substr(cmcode,1,8)
**合并8位
merge m:1 cmcode8 using "trade_war_product_list_hs8_supplement.dta",update 
drop if _merge==2 
drop cmcode8-_merge

gen cmcode6=substr(cmcode,1,6)
**合并6位
merge m:1 cmcode6 using "trade_war_product_list_hs6_supplement.dta",update
drop if _merge==2 
drop _merge 

* 34 billion round: annouced on June 15, 2018, enforced on July 6, 2018, tariff rate + 25% 
* 16 billion round: annouced on Aug 7, 2018, enforced on Aug 23, 2018, tariff rate + 25%
* 200 billion round: annouced on July 10, 2018, enforced on Sep 18, 2018, tariff rate + 10%

gen tariff_round=0
replace tariff_round=34 if 加征关税批次=="340亿商品"
replace tariff_round=16 if 加征关税批次=="160亿商品"
replace tariff_round=200 if 加征关税批次=="2000亿商品"	

drop if cmcode==" "
drop 加征关税批次
  
* Merge 6-digit tariff rates
merge m:1 cmcode6 cty_id year using import_tariff_6digit_fill.dta
drop if _merge==2
drop _merge cmcode6 
replace tariff_rate=0 if tariff_rate==. // set the very few unmatched missing values of tariff rates as zero
  
* Adjust tariff rate for exports to the US using the tariff rate changes during trade war
replace tariff_rate=tariff_rate+25 if cty_id==502 & tariff_round==34 & ym>=tm(2018m7)  
replace tariff_rate=tariff_rate+25 if cty_id==502 & tariff_round==16 & ym>=tm(2018m8)  
replace tariff_rate=tariff_rate+10 if cty_id==502 & tariff_round==200 & ym>=tm(2018m9) 
replace tariff_rate=tariff_rate+15 if cty_id==502 & tariff_round==200 & ym>=tm(2019m5)  
 
* generate HS-8 variable
gen cmcode8=substr(cmcode,1,8)
egen g8=group(cmcode8)
drop cmcode8
   
egen gt = group(cmcode ym)  
egen ct = group(cty_id ym)  
egen cg = group(cty_id cmcode)
egen g  = group(cmcode)
egen f  = group(firm)
egen fg = group(firm cmcode)  
egen fc = group(firm cty_id)  
egen fcg= group(firm cmcode cty_id) 
egen fgt= group(firm cmcode ym) 
egen fct= group(firm cty_id ym) 
 
gen lnexport=ln(exp_usd)
gen ln_p=ln(exp_usd/bg_q)
gen ln_q=ln(bg_q)

drop cmcode bg_q year tariff_round

gen lnTariff=ln(1+tariff_rate/100)  

* Table 4 *
* Estimating elasticities
qui reghdfe lnexport lnTariff, a(gt ct cg) cluster(g8)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) sdec(4) nocons onecol replace 
qui reghdfe lnexport lnTariff, a(gt ct cg f) cluster(g8)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) sdec(4) nocons onecol append 
qui reghdfe lnexport lnTariff, a(gt ct cg fg fc) cluster(g8)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) sdec(4) nocons onecol append 
qui reghdfe lnexport lnTariff, a(gt ct fcg) cluster(g8)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) sdec(4) nocons onecol append 
qui reghdfe lnexport lnTariff, a(fcg fgt fct) cluster(g8)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) sdec(4) nocons onecol append 

qui reghdfe ln_p lnTariff, a(gt ct cg) cluster(g8)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) sdec(4) nocons onecol append 
qui reghdfe ln_p lnTariff, a(gt ct cg f) cluster(g8)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) sdec(4) nocons onecol append 
qui reghdfe ln_p lnTariff, a(gt ct cg fg fc) cluster(g8)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) sdec(4) nocons onecol append 
qui reghdfe ln_p lnTariff, a(gt ct fcg) cluster(g8)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) sdec(4) nocons onecol append 
qui reghdfe ln_p lnTariff, a(fcg fgt fct) cluster(g8)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) sdec(4) nocons onecol append 

qui reghdfe ln_q lnTariff, a(gt ct cg) cluster(g8)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) sdec(4) nocons onecol append 
qui reghdfe ln_q lnTariff, a(gt ct cg f) cluster(g8)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) sdec(4) nocons onecol append 
qui reghdfe ln_q lnTariff, a(gt ct cg fg fc) cluster(g8)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) sdec(4) nocons onecol append 
qui reghdfe ln_q lnTariff, a(gt ct fcg) cluster(g8)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) sdec(4) nocons onecol append 
qui reghdfe ln_q lnTariff, a(fcg fgt fct) cluster(g8)
outreg2 using tariff.xls, keep(lnTariff) adjr2 bdec(4) sdec(4) nocons onecol append 
  

*************************************************************************************************************
// Examine whether firms with negative profits adjust export prices differently from those with positive profits //
									** Firm-Commodity-Country-level analysis **
* Caution: Running regressions in this part will take a long time 
*************************************************************************************************************  

use export_merged.dta, clear 
  
keep firm ym year cmcode cty_id exp_usd bg_q 
keep if ym>=tm(2017m1) & ym<=tm(2019m6)  
drop if cty_id==142 // drop "exports" to China
 
collapse (sum) exp_usd bg_q (mean) year, by(firm cmcode cty_id ym) 
 
* Merge with 中美贸易战商品代码清单 (Following Zhikuo's work)      
**合并10位
merge m:1 cmcode using "trade_war_product_list_hs10.dta", update
drop if _merge==2 
drop 序号 中国海关税则号 中国海关商品名称 length_cmcode cmcode10 _merge 

gen cmcode8=substr(cmcode,1,8)
**合并8位
merge m:1 cmcode8 using "trade_war_product_list_hs8_supplement.dta",update 
drop if _merge==2 
drop cmcode8-_merge

gen cmcode6=substr(cmcode,1,6)
**合并6位
merge m:1 cmcode6 using "trade_war_product_list_hs6_supplement.dta",update
drop if _merge==2 
drop _merge 

* 34 billion round: annouced on June 15, 2018, enforced on July 6, 2018, tariff rate + 25% 
* 16 billion round: annouced on Aug 7, 2018, enforced on Aug 23, 2018, tariff rate + 25%
* 200 billion round: annouced on July 10, 2018, enforced on Sep 18, 2018, tariff rate + 10%

gen tariff_round=0
replace tariff_round=34 if 加征关税批次=="340亿商品"
replace tariff_round=16 if 加征关税批次=="160亿商品"
replace tariff_round=200 if 加征关税批次=="2000亿商品"	

drop if cmcode==" "
drop 加征关税批次
  
* Merge 6-digit tariff rates
merge m:1 cmcode6 cty_id year using import_tariff_6digit_fill.dta
drop if _merge==2
drop _merge  
replace tariff_rate=0 if tariff_rate==. // set the very few unmatched missing values of tariff rates as zero
  
* Adjust tariff rate for exports to the US using the tariff rate changes during trade war
replace tariff_rate=tariff_rate+25 if cty_id==502 & tariff_round==34 & ym>=tm(2018m7) 
replace tariff_rate=tariff_rate+25 if cty_id==502 & tariff_round==16 & ym>=tm(2018m8)  
replace tariff_rate=tariff_rate+10 if cty_id==502 & tariff_round==200 & ym>=tm(2018m9) 
 
* generate HS-8 variable
gen cmcode8=substr(cmcode,1,8)
egen g8=group(cmcode8)
drop cmcode8
   
egen gt = group(cmcode ym)  
egen ct = group(cty_id ym)  
egen cg = group(cty_id cmcode)
egen g  = group(cmcode)
egen f  = group(firm)
egen fg = group(firm cmcode)  
egen fc = group(firm cty_id)  
*egen ft = group(firm ym) 
egen fcg= group(firm cmcode cty_id) 
egen fgt= group(firm cmcode ym) 
egen fct= group(firm cty_id ym) 
 
gen lnexport=ln(exp_usd)
gen ln_p=ln(exp_usd/bg_q)
gen ln_q=ln(bg_q)

drop cmcode bg_q  

gen lnTariff=ln(1+tariff_rate/100)  


// Examine whether firms with negative profits adjust export prices differently from those with positive profits //
 
* Merge the profit information 
* Prepare quarterly profits data
preserve
use firm_quarterly_profits.dta, clear  
gen q_start = qofd(dofm(ym_start))
gen q_end   = qofd(dofm(ym_end))
keep if q_start==q_end
ren q_start yq
format %tq yq    
keep firm revenue cost profit yq
sort firm yq
keep if yq>=tq(2017q1) & yq<=tq(2019q2) //  Time span: 2017q1-2019q2, need to ensure the monthly data are from 2017m1-2019m6
egen f  = group(firm)
xtset f yq  
gen lag_profit=L.profit
save quarter_temp.dta, replace 
restore 
  
gen yq = qofd(dofm(ym))
format %tq yq  
   
merge m:1 firm yq using quarter_temp.dta
drop if _merge==2
drop _merge
erase quarter_temp.dta  
    

* Examine whether export prices to different countries also adjust less when a firm's profit is negative
 
/* Impact of U.S. tariff rates on prices of Chinese exports to different markets. */
* Divide exporting destinations as 5 groups: (0) US (1) ASEAN (2) EU (3) Japan and Korea (4) Hong Kong, Taiwan, Macau (5) ROW
gen exp_g=5
replace exp_g=0 if cty_id==502
* Singapore 132, Brunei 105, Malaysia 122, Thailand 136, Phillippines 129, Indonesia 112, Vietnam 141, Laos 119, Myanmar 106, and Cambodia 107
replace exp_g=1 if cty_id==132 | cty_id==105 | cty_id==122 | cty_id==136 | cty_id==129 | cty_id==112 | cty_id==141 | cty_id==119 | cty_id==106 | cty_id==107
replace exp_g=2 if cty_id==108 | (cty_id>=301 & cty_id<=312) | cty_id==315 | cty_id==316 | cty_id==318 | cty_id==321 | cty_id==324 | cty_id==327 | cty_id==328 | cty_id==330 | (cty_id>=334 & cty_id<=336) | (cty_id>=350 & cty_id<=353)
replace exp_g=3 if cty_id==116 | cty_id==133
replace exp_g=4 if cty_id==110 | cty_id==121 | cty_id==143 
  
* generate the US tariff rate for each commodity in each month 
preserve
use import_tariff_6digit_fill.dta, clear 
keep if cty_id==502
ren tariff_rate tariff_rate_US
save import_tariff_6digit_fill_US.dta, replace
restore  

merge m:1 cmcode6 year using import_tariff_6digit_fill_US.dta
drop if _merge==2
drop _merge  
replace tariff_rate_US=0 if tariff_rate_US==. // set the very few unmatched missing values of tariff rates as zero

* Adjust tariff rate for exports to the US using the tariff rate changes during trade war
replace tariff_rate_US=tariff_rate_US+25 if tariff_round==34 & ym>=tm(2018m7)  
replace tariff_rate_US=tariff_rate_US+25 if tariff_round==16 & ym>=tm(2018m8) 
replace tariff_rate_US=tariff_rate_US+10 if tariff_round==200 & ym>=tm(2018m9)  
 
gen tariff_rate_US_g0=tariff_rate_US*(exp_g==0)
gen tariff_rate_US_g1=tariff_rate_US*(exp_g==1)
gen tariff_rate_US_g2=tariff_rate_US*(exp_g==2)
gen tariff_rate_US_g3=tariff_rate_US*(exp_g==3)
gen tariff_rate_US_g4=tariff_rate_US*(exp_g==4)
gen tariff_rate_US_g5=tariff_rate_US*(exp_g==5) 

gen neg_profit=(lag_profit<0) 
gen neg_profit_tariff_rate_US_g0=neg_profit*tariff_rate_US*(exp_g==0)
gen neg_profit_tariff_rate_US_g1=neg_profit*tariff_rate_US*(exp_g==1)
gen neg_profit_tariff_rate_US_g2=neg_profit*tariff_rate_US*(exp_g==2)
gen neg_profit_tariff_rate_US_g3=neg_profit*tariff_rate_US*(exp_g==3)
gen neg_profit_tariff_rate_US_g4=neg_profit*tariff_rate_US*(exp_g==4)
gen neg_profit_tariff_rate_US_g5=neg_profit*tariff_rate_US*(exp_g==5)
 
* Regression: response of exports to different destinations
qui reghdfe ln_p tariff_rate tariff_rate_US_g0-neg_profit_tariff_rate_US_g5, a(ct cg) cluster(g)
outreg2 using tariff.xls, keep(tariff_rate tariff_rate_US_g0-neg_profit_tariff_rate_US_g5) adjr2 bdec(4) nocons onecol replace 
   

	
*************************************************************************************************************
										** Firm-level analysis **
									* Monthly variables: exports and sales *
*************************************************************************************************************
 
use firm_monthly_all_infor.dta, clear  

keep if ym>=tm(2017m1) & ym<=tm(2019m4)
egen f=group(firm)
xtset f ym  
 
* Ensure internal consistency of the definition of various sales value
merge m:1 ym using exchange_rate.dta // merge exchange rate information
drop if _merge==2
drop _merge
 
replace exp_US=0 if exp_US==.
replace exp_non_US=0 if exp_non_US==.
replace sales_domestic=0 if sales_domestic==.   
replace sales_export=exp_US+exp_non_US
replace sales_domestic=sales_domestic/exchange_rate // converting RMB sales to USD sales
replace sales_total=sales_export+sales_domestic

*Use the inverse of the hyperbolic sine transformation to address zero values: log(x+(x^2+1)^0.5)
foreach var of varlist exp_US-sales_domestic{
	gen ln_`var'=ln(`var'+((`var')^2+1)^0.5)
} 
 
* Table 1. characteristics of firms 
preserve 
bysort firm: gen seq=_n
keep if seq==1
drop seq
tab firm_type, sort
tab ind_name, sort 
tab ownership, sort
restore 
 
* Summary statistics of key variables 
sum tariff_rate_fm tariff_rate_fq exp_US exp_non_US sales_export sales_domestic sales_total ln_exp_US ln_exp_non_US ln_sales_export ln_sales_domestic ln_sales_total

* Table 2 
preserve
keep if year==2017
collapse (sum) exp_US exp_non_US sales_export sales_domestic sales_total, by(firm)

foreach var of varlist exp_US exp_non_US sales_export sales_domestic sales_total{
  replace `var'=`var'/1000000  // convert all values to million current US dollars
}
  
estpost sum exp_US exp_non_US sales_export sales_domestic sales_total, de  
esttab using temp.smcl, cells("count mean sd min max p25 p50 p75") replace
restore

preserve
keep if year==2017
collapse (sum) exp_US exp_non_US sales_export sales_domestic sales_total, by(firm)

gen ln_exp_US=ln(exp_US)
gen ln_exp_non_US=ln(exp_non_US)
gen ln_sales_export=ln(sales_export) 
estpost sum ln_exp_US ln_exp_non_US ln_sales_export, de  
esttab using temp.smcl, cells("count mean sd min max p25 p50 p75") replace
 
restore 
   
// Event-study regressions 
* Figure A4 * 
gen event_time=.
replace event_time=ym-tm(2018m7) if treat==1

foreach num of numlist 18(-1)1{
	gen treat_pre_`num'=0
	replace treat_pre_`num'=1 if event_time*(-1)==`num'  
}
foreach num of numlist 0(1)9{
	gen treat_post_`num'=0
	replace treat_post_`num'=1 if event_time==`num'  
} 
 
* set omitted group
replace treat_pre_1=0
 
preserve // evolution of firm tariff rate IV
qui reghdfe tariff_rate_fm_IV  treat_pre_* treat_post_*, a(f ym) cluster(f) 
estimates store event  
coefplot event, omitted keep(treat_pre_* treat_post_*) rename(treat_pre_*="- " treat_post_*=" ") ///
vertical graphregion(color(white)) lwidth(*2) recast(line) ciopts(recast(rline) lpattern(dash)) ///
ytitle("") xtitle(month after tariff increase)  
graph export Fig-Tariff_IV_event.eps, replace  
restore  
  
preserve // evolution of firm tariff rate 
qui reghdfe tariff_rate_fm  treat_pre_* treat_post_*, a(f ym) cluster(f) 
estimates store event  
coefplot event, omitted keep(treat_pre_* treat_post_*) rename(treat_pre_*="- " treat_post_*=" ") ///
vertical graphregion(color(white)) yline(0, lpattern(dash))  ///
ytitle("") xtitle(month after tariff increase) 
graph export Fig-Tariff_event.eps, replace  
restore     
 
// evolution of firm sales and exports 
preserve 
qui reghdfe ln_exp_US  treat_pre_* treat_post_*, a(f ym) cluster(f) 
estimates store event  
coefplot event, omitted keep(treat_pre_* treat_post_*) rename(treat_pre_*="- " treat_post_*=" ") ///
vertical graphregion(color(white)) yline(0, lpattern(dash)) ///
ytitle("") xtitle(month after tariff increase)  
graph export Fig-exp_US_event.eps, replace  
restore    

preserve 
qui reghdfe ln_exp_non_US  treat_pre_* treat_post_*, a(f ym) cluster(f) 
estimates store event  
coefplot event, omitted keep(treat_pre_* treat_post_*) rename(treat_pre_*="- " treat_post_*=" ") ///
vertical graphregion(color(white)) yline(0, lpattern(dash)) ///
ytitle("") xtitle(month after tariff increase)  ylabel(-0.15(0.05)0.15) 
graph export Fig-exp_nonUS_event.eps, replace  
restore    
 
preserve 
qui reghdfe ln_sales_domestic  treat_pre_* treat_post_*, a(f ym) cluster(f) 
estimates store event  
coefplot event, omitted keep(treat_pre_* treat_post_*) rename(treat_pre_*="- " treat_post_*=" ") ///
vertical graphregion(color(white)) yline(0, lpattern(dash)) ///
ytitle("") xtitle(month after tariff increase) ylabel(-0.2(0.1)0.2) 
graph export Fig-sales_domestic_event.eps, replace  
restore    

preserve 
qui reghdfe ln_sales_export  treat_pre_* treat_post_*, a(f ym) cluster(f) 
estimates store event  
coefplot event, omitted keep(treat_pre_* treat_post_*) rename(treat_pre_*="- " treat_post_*=" ") ///
vertical graphregion(color(white)) yline(0, lpattern(dash)) ///
ytitle("") xtitle(month after tariff increase) ylabel(-0.2(0.1)0.2)
graph export Fig-sales_export_event.eps, replace  
restore    

preserve 
qui reghdfe ln_sales_total  treat_pre_* treat_post_*, a(f ym) cluster(f) 
estimates store event  
coefplot event, omitted keep(treat_pre_* treat_post_*) rename(treat_pre_*="- " treat_post_*=" ") ///
vertical graphregion(color(white)) yline(0, lpattern(dash)) ///
ytitle("") xtitle(month after tariff increase) ylabel(-0.15(0.05)0.15) 
graph export Fig-sales_total_event.eps, replace  
restore       
   
  
/*   Install ivreghdfe if not previously installed
* Install ftools (remove program if it existed previously)
cap ado uninstall ftools
ssc install ftools 

* Install reghdfe
cap ado uninstall reghdfe
ssc install reghdfe 

* Install boottest (Stata 11 and 12)
if (c(version)<13) cap ado uninstall boottest
if (c(version)<13) ssc install boottest

* Install moremata (sometimes used by ftools but not needed for reghdfe)
cap ssc install moremata

* Install ivreg2, the core package
cap ado uninstall ivreg2
ssc install ivreg2

* Finally, install this package
cap ado uninstall ivreghdfe
ssc install ivreghdfe   
*/
  
* Regressions: impact of weighted tariff rates on firms' exports and sales
* Table 5 * 
 
gen lnTariff=ln(1+tariff_rate_fm_IV/100) 
replace ln_exp_US=0 if ln_exp_US==.
replace ln_exp_non_US=0 if ln_exp_non_US==.
replace ln_sales_export=0 if ln_sales_export==.
replace ln_sales_domestic=0 if ln_sales_domestic==.
replace ln_sales_total=0 if ln_sales_total==.
 
* All sample: reduced-form estimates 
qui reghdfe ln_exp_US lnTariff, absorb(f ym) cluster(f)  
estat sum  
matrix matsum = r(stats)
scalar mean_evasion = matsum[1,1]
scalar sd_evasion = matsum[1,2]
outreg2 using tariff.xls,   keep(lnTariff) addstat("mean(DV)", mean_evasion, "std(DV)", sd_evasion) bdec(3) nocons onecol label replace
 
qui reghdfe ln_exp_non_US lnTariff, absorb(f ym) cluster(f)  
estat sum  
matrix matsum = r(stats)
scalar mean_evasion = matsum[1,1]
scalar sd_evasion = matsum[1,2]
outreg2 using tariff.xls,   keep(lnTariff) addstat("mean(DV)", mean_evasion, "std(DV)", sd_evasion) bdec(3) nocons onecol label append

qui reghdfe ln_sales_export lnTariff, absorb(f ym) cluster(f)  
estat sum  
matrix matsum = r(stats)
scalar mean_evasion = matsum[1,1]
scalar sd_evasion = matsum[1,2]
outreg2 using tariff.xls,   keep(lnTariff) addstat("mean(DV)", mean_evasion, "std(DV)", sd_evasion) bdec(3) nocons onecol label append

qui reghdfe ln_sales_domestic lnTariff, absorb(f ym) cluster(f)  
estat sum  
matrix matsum = r(stats)
scalar mean_evasion = matsum[1,1]
scalar sd_evasion = matsum[1,2]
outreg2 using tariff.xls,   keep(lnTariff) addstat("mean(DV)", mean_evasion, "std(DV)", sd_evasion) bdec(3) nocons onecol label append

qui reghdfe ln_sales_total lnTariff, absorb(f ym) cluster(f)  
estat sum  
matrix matsum = r(stats)
scalar mean_evasion = matsum[1,1]
scalar sd_evasion = matsum[1,2]
outreg2 using tariff.xls,   keep(lnTariff) addstat("mean(DV)", mean_evasion, "std(DV)", sd_evasion) bdec(3) nocons onecol label append
   
* Table A2 * 	 
* trade intermediaries 外贸企业
qui reghdfe ln_exp_US         lnTariff if firm_type==1, absorb(f ym) cluster(f)
outreg2 using tariff.xls,   keep(lnTariff) bdec(3) nocons onecol label replace 
qui reghdfe ln_exp_non_US     lnTariff if firm_type==1, absorb(f ym) cluster(f)
outreg2 using tariff.xls,   keep(lnTariff) bdec(3) nocons onecol label append 
qui reghdfe ln_sales_export   lnTariff if firm_type==1, absorb(f ym) cluster(f)
outreg2 using tariff.xls,   keep(lnTariff) bdec(3) nocons onecol label append
qui reghdfe ln_sales_domestic lnTariff if firm_type==1, absorb(f ym) cluster(f)
outreg2 using tariff.xls,   keep(lnTariff) bdec(3) nocons onecol label append  
qui reghdfe ln_sales_total    lnTariff if firm_type==1, absorb(f ym) cluster(f)
outreg2 using tariff.xls,   keep(lnTariff) bdec(3) nocons onecol label append
 
* direct exporters 生产企业
qui reghdfe ln_exp_US         lnTariff if firm_type==0, absorb(f ym) cluster(f)
outreg2 using tariff.xls,   keep(lnTariff) bdec(3) nocons onecol label replace 
qui reghdfe ln_exp_non_US     lnTariff if firm_type==0, absorb(f ym) cluster(f)
outreg2 using tariff.xls,   keep(lnTariff) bdec(3) nocons onecol label append 
qui reghdfe ln_sales_export   lnTariff if firm_type==0, absorb(f ym) cluster(f)
outreg2 using tariff.xls,   keep(lnTariff) bdec(3) nocons onecol label append
qui reghdfe ln_sales_domestic lnTariff if firm_type==0, absorb(f ym) cluster(f)
outreg2 using tariff.xls,   keep(lnTariff) bdec(3) nocons onecol label append  
qui reghdfe ln_sales_total    lnTariff if firm_type==0, absorb(f ym) cluster(f)
outreg2 using tariff.xls,   keep(lnTariff) bdec(3) nocons onecol label append
 
   
*************************************************************************************************************
								      ** Firm-level analysis  **
							        * Quarterly variable: profits *
*************************************************************************************************************

* Prepare quarterly profits data
use firm_quarterly_profits.dta, clear  
gen q_start = qofd(dofm(ym_start))
gen q_end   = qofd(dofm(ym_end))
keep if q_start==q_end
ren q_start yq
format %tq yq    
keep firm revenue cost profit yq
sort firm yq
keep if yq>=tq(2017q1) & yq<=tq(2019q1) //  Time span: 2017q1-2019q1, need to ensure the monthly data are from 2017m1-2019m3
save quarter_temp.dta, replace

* Prepare quarterly other information from firm monthly data
use firm_monthly_all_infor.dta, clear  
keep if ym>=tm(2017m1) & ym<=tm(2019m3)

* Ensure internal consistency of the definition of various sales value
merge m:1 ym using exchange_rate.dta // merge exchange rate information
drop if _merge==2
drop _merge
 
replace exp_US=0 if exp_US==.
replace exp_non_US=0 if exp_non_US==.
replace sales_domestic=0 if sales_domestic==.   
replace sales_export=exp_US+exp_non_US
replace sales_domestic=sales_domestic/exchange_rate // converting RMB sales to USD sales
replace sales_total=sales_export+sales_domestic
     
* convert exchange rate to quarterly
bysort yq: egen temp=mean(exchange_rate)
replace  exchange_rate=temp
drop temp

* generate quarterly exports and sales
sort firm yq 
foreach var of varlist exp_US-sales_domestic{
	by firm yq: egen temp=sum(`var')
	replace `var'=temp
	drop temp
	gen ln_`var'=ln(`var') 

}  
 
by firm yq: gen seq=_n
keep if seq==1
drop seq

merge 1:1 firm yq using quarter_temp.dta
drop if _merge==2
drop _merge
erase quarter_temp.dta  
  
egen f=group(firm)
xtset f yq  

replace profit=profit/exchange_rate // converting RMB sales to USD sales
replace revenue=revenue/exchange_rate
gen profit_margin=profit/revenue
winsor profit_margin, gen(profit_margin_ws) p(0.01) // For profit margin, we winsorize the top and bottom 1% values to address extreme values.
 
* Summary statistics of key variables 
sum tariff_rate_fq profit_margin_ws
				      
* Regressions: impact of weighted tariff rates on firms' exports and sales and profitability 
* Table 5 (profits) *
* All sample: reduced-form estimates 

gen lnTariff=ln(1+tariff_rate_fq_IV/100)

qui reghdfe profit_margin_ws lnTariff, absorb(f yq) cluster(f)  
estat sum  
matrix matsum = r(stats)
scalar mean_evasion = matsum[1,1]
scalar sd_evasion = matsum[1,2]
outreg2 using tariff.xls,   keep(lnTariff) addstat("mean(DV)", mean_evasion, "std(DV)", sd_evasion) bdec(3) nocons onecol label replace
  
* Table A2 (profits) * 
* trade intermediaries 外贸企业
qui reghdfe profit_margin_ws  lnTariff if firm_type==1, absorb(f yq) cluster(f)
outreg2 using tariff.xls,   keep(lnTariff) bdec(3) nocons onecol label append  
 
* direct exporters 生产企业  
qui reghdfe profit_margin_ws  lnTariff if firm_type==0, absorb(f yq) cluster(f)
outreg2 using tariff.xls,   keep(lnTariff) bdec(3) nocons onecol label append   
 
    

*************************************************************************************************************
	       **  Adjustments of firm-level sales in different markets **   
*************************************************************************************************************  

use export_merged.dta, clear 
  
keep firm ym cty_id exp_usd 
keep if ym>=tm(2017m1) & ym<=tm(2019m4)  
collapse (sum) exp_usd, by(firm cty_id ym) 
gen lnexp=ln(exp_usd)
 
* Merge with firm-level tariff rates and other firm level information  
merge m:n firm ym using firm_monthly_all_infor.dta
keep if _merge==3
drop _merge

* Ensure internal consistency of the definition of various sales value
merge m:1 ym using exchange_rate.dta // merge exchange rate information
drop if _merge==2
drop _merge  
replace sales_domestic=sales_domestic/exchange_rate // converting RMB sales to USD sales

*Use the inverse of the hyperbolic sine transformation to address zero values: log(x+(x^2+1)^0.5)
foreach var of varlist exp_US-sales_domestic{
	gen ln_`var'=ln(`var'+((`var')^2+1)^0.5)
} 
 
* Keep necessary variables and transform domestic sales infor to the exports variable as sales in China
keep firm cty_id ym lnexp tariff_rate_fm_IV tariff_rate_fm ln_sales_domestic
*replace ln_sales_domestic=0 if ln_sales_domestic==.

egen fc = group(firm cty_id)  
xtset fc ym
tsfill, full  
 
bysort fc (ym):    carryforward firm cty_id, replace  
gen int negym = -ym  
bysort fc (negym): carryforward firm cty_id, replace
drop negym
sort firm ym cty_id
by firm ym: egen temp=min(ln_sales_domestic)
replace ln_sales_domestic=temp
drop temp
replace lnexp=ln_sales_domestic if cty_id==142 // replace the value for "exports" to China by value of domestic sales
drop ln_sales_domestic
drop if lnexp==.

by firm ym: egen temp=min(tariff_rate_fm_IV)
replace tariff_rate_fm_IV=temp
drop temp

by firm ym: egen temp=min(tariff_rate_fm)
replace tariff_rate_fm=temp
drop temp
 
* Divide exporting destinations (including China as a special group) as 6 groups: (6) China (0) US (1) ASEAN (2) EU (3) Japan and Korea (4) Hong Kong, Taiwan, Macau (5) ROW
gen exp_g=5
replace exp_g=6 if cty_id==142
replace exp_g=0 if cty_id==502
* Singapore 132, Brunei 105, Malaysia 122, Thailand 136, Phillippines 129, Indonesia 112, Vietnam 141, Laos 119, Myanmar 106, and Cambodia 107
replace exp_g=1 if cty_id==132 | cty_id==105 | cty_id==122 | cty_id==136 | cty_id==129 | cty_id==112 | cty_id==141 | cty_id==119 | cty_id==106 | cty_id==107
replace exp_g=2 if cty_id==108 | (cty_id>=301 & cty_id<=312) | cty_id==315 | cty_id==316 | cty_id==318 | cty_id==321 | cty_id==324 | cty_id==327 | cty_id==328 | cty_id==330 | (cty_id>=334 & cty_id<=336) | (cty_id>=350 & cty_id<=353)
replace exp_g=3 if cty_id==116 | cty_id==133
replace exp_g=4 if cty_id==110 | cty_id==121 | cty_id==143 
 
egen f=group(firm)
egen ct = group(cty_id ym)  
   
* Regression: response of sales in different markets
  
* Table 6 *
preserve    
gen tariff_rate_fm_g0=tariff_rate_fm*(exp_g==0)
gen tariff_rate_fm_g1=tariff_rate_fm*(exp_g==1)
gen tariff_rate_fm_g2=tariff_rate_fm*(exp_g==2)
gen tariff_rate_fm_g3=tariff_rate_fm*(exp_g==3)
gen tariff_rate_fm_g4=tariff_rate_fm*(exp_g==4)
gen tariff_rate_fm_g5=tariff_rate_fm*(exp_g==5)
gen tariff_rate_fm_g6=tariff_rate_fm*(exp_g==6)
 
gen tariff_rate_fm_IV_g0=tariff_rate_fm_IV*(exp_g==0)
gen tariff_rate_fm_IV_g1=tariff_rate_fm_IV*(exp_g==1)
gen tariff_rate_fm_IV_g2=tariff_rate_fm_IV*(exp_g==2)
gen tariff_rate_fm_IV_g3=tariff_rate_fm_IV*(exp_g==3)
gen tariff_rate_fm_IV_g4=tariff_rate_fm_IV*(exp_g==4)
gen tariff_rate_fm_IV_g5=tariff_rate_fm_IV*(exp_g==5)
gen tariff_rate_fm_IV_g6=tariff_rate_fm_IV*(exp_g==6) 

gen lnTariff_g0=ln(1+tariff_rate_fm_IV_g0/100)  
gen lnTariff_g1=ln(1+tariff_rate_fm_IV_g1/100)  
gen lnTariff_g2=ln(1+tariff_rate_fm_IV_g2/100)  
gen lnTariff_g3=ln(1+tariff_rate_fm_IV_g3/100)  
gen lnTariff_g4=ln(1+tariff_rate_fm_IV_g4/100)  
gen lnTariff_g5=ln(1+tariff_rate_fm_IV_g5/100)  
gen lnTariff_g6=ln(1+tariff_rate_fm_IV_g6/100)  
   
qui reghdfe lnexp lnTariff_g*, absorb(ct fc) cluster(fc)
estat sum  
matrix matsum = r(stats)
scalar mean_evasion = matsum[1,1]
scalar sd_evasion = matsum[1,2]
outreg2 using tariff.xls,   keep(lnTariff_g*) addstat("mean(DV)", mean_evasion, "std(DV)", sd_evasion) bdec(4) sdec(4) nocons onecol label append
   
restore
 
 
/* Explore the overlap of Chinese exporting commodities to the US and other destinations */
* Table A4 and Table A5 *

* Based on 10-digit HS code
use export_merged.dta, clear 

keep if ym>=tm(2014m1) & ym<=tm(2016m12)  // focus on initial period 2014-16
keep cmcode cty_id 
drop if cty_id==142 // drop "exports" to China

* Divide exporting destinations as 5 groups: (0) US (1) ASEAN (2) EU (3) Japan and Korea (4) Hong Kong, Taiwan, Macau (5) ROW
gen exp_g=5
replace exp_g=0 if cty_id==502
* Singapore 132, Brunei 105, Malaysia 122, Thailand 136, Phillippines 129, Indonesia 112, Vietnam 141, Laos 119, Myanmar 106, and Cambodia 107
replace exp_g=1 if cty_id==132 | cty_id==105 | cty_id==122 | cty_id==136 | cty_id==129 | cty_id==112 | cty_id==141 | cty_id==119 | cty_id==106 | cty_id==107
replace exp_g=2 if cty_id==108 | (cty_id>=301 & cty_id<=312) | cty_id==315 | cty_id==316 | cty_id==318 | cty_id==321 | cty_id==324 | cty_id==327 | cty_id==328 | cty_id==330 | (cty_id>=334 & cty_id<=336) | (cty_id>=350 & cty_id<=353)
replace exp_g=3 if cty_id==116 | cty_id==133
replace exp_g=4 if cty_id==110 | cty_id==121 | cty_id==143 
drop cty_id
bysort cmcode exp_g: gen seq=_n
keep if seq==1
drop seq

foreach num of numlist 0(1)5{
gen exp_g`num'=(exp_g==`num') 
by cmcode: egen exp_g`num'_max=max(exp_g`num')
drop exp_g`num'
} 

by cmcode: gen seq=_n
keep if seq==1
drop seq exp_g

foreach num of numlist 1(1)5{
gen exp_US_OR_`num' =(exp_g0_max==1 | exp_g`num'_max==1) 
gen exp_US_AND_`num'=(exp_g0_max==1 & exp_g`num'_max==1) 
} 

foreach num of numlist 1(1)5{
qui egen exp_US_OR_`num'_sum =sum(exp_US_OR_`num')
qui egen exp_US_AND_`num'_sum=sum(exp_US_AND_`num') 
qui gen exp_US_`num'_ratio=exp_US_AND_`num'_sum/exp_US_OR_`num'_sum
tab exp_US_`num'_ratio
}  

* Based on 8-digit HS code
use export_merged.dta, clear 

keep if ym>=tm(2014m1) & ym<=tm(2016m12)  // focus on initial period 2014-16
keep cmcode cty_id 
gen cmcode8=substr(cmcode,1,8)
drop cmcode
ren cmcode8 cmcode
drop if cty_id==142 // drop "exports" to China

* Divide exporting destinations as 5 groups: (0) US (1) ASEAN (2) EU (3) Japan and Korea (4) Hong Kong, Taiwan, Macau (5) ROW
gen exp_g=5
replace exp_g=0 if cty_id==502
* Singapore 132, Brunei 105, Malaysia 122, Thailand 136, Phillippines 129, Indonesia 112, Vietnam 141, Laos 119, Myanmar 106, and Cambodia 107
replace exp_g=1 if cty_id==132 | cty_id==105 | cty_id==122 | cty_id==136 | cty_id==129 | cty_id==112 | cty_id==141 | cty_id==119 | cty_id==106 | cty_id==107
replace exp_g=2 if cty_id==108 | (cty_id>=301 & cty_id<=312) | cty_id==315 | cty_id==316 | cty_id==318 | cty_id==321 | cty_id==324 | cty_id==327 | cty_id==328 | cty_id==330 | (cty_id>=334 & cty_id<=336) | (cty_id>=350 & cty_id<=353)
replace exp_g=3 if cty_id==116 | cty_id==133
replace exp_g=4 if cty_id==110 | cty_id==121 | cty_id==143 
drop cty_id
bysort cmcode exp_g: gen seq=_n
keep if seq==1
drop seq

foreach num of numlist 0(1)5{
gen exp_g`num'=(exp_g==`num') 
by cmcode: egen exp_g`num'_max=max(exp_g`num')
drop exp_g`num'
} 

by cmcode: gen seq=_n
keep if seq==1
drop seq exp_g

foreach num of numlist 1(1)5{
gen exp_US_OR_`num' =(exp_g0_max==1 | exp_g`num'_max==1) 
gen exp_US_AND_`num'=(exp_g0_max==1 & exp_g`num'_max==1) 
} 

foreach num of numlist 1(1)5{
qui egen exp_US_OR_`num'_sum =sum(exp_US_OR_`num')
qui egen exp_US_AND_`num'_sum=sum(exp_US_AND_`num') 
qui gen exp_US_`num'_ratio=exp_US_AND_`num'_sum/exp_US_OR_`num'_sum
tab exp_US_`num'_ratio
}  

* Based on 6-digit HS code
use export_merged.dta, clear 

keep if ym>=tm(2014m1) & ym<=tm(2016m12)  // focus on initial period 2014-16
keep cmcode cty_id 
gen cmcode6=substr(cmcode,1,6)
drop cmcode
ren cmcode6 cmcode
drop if cty_id==142 // drop "exports" to China

* Divide exporting destinations as 5 groups: (0) US (1) ASEAN (2) EU (3) Japan and Korea (4) Hong Kong, Taiwan, Macau (5) ROW
gen exp_g=5
replace exp_g=0 if cty_id==502
* Singapore 132, Brunei 105, Malaysia 122, Thailand 136, Phillippines 129, Indonesia 112, Vietnam 141, Laos 119, Myanmar 106, and Cambodia 107
replace exp_g=1 if cty_id==132 | cty_id==105 | cty_id==122 | cty_id==136 | cty_id==129 | cty_id==112 | cty_id==141 | cty_id==119 | cty_id==106 | cty_id==107
replace exp_g=2 if cty_id==108 | (cty_id>=301 & cty_id<=312) | cty_id==315 | cty_id==316 | cty_id==318 | cty_id==321 | cty_id==324 | cty_id==327 | cty_id==328 | cty_id==330 | (cty_id>=334 & cty_id<=336) | (cty_id>=350 & cty_id<=353)
replace exp_g=3 if cty_id==116 | cty_id==133
replace exp_g=4 if cty_id==110 | cty_id==121 | cty_id==143 
drop cty_id
bysort cmcode exp_g: gen seq=_n
keep if seq==1
drop seq

foreach num of numlist 0(1)5{
gen exp_g`num'=(exp_g==`num') 
by cmcode: egen exp_g`num'_max=max(exp_g`num')
drop exp_g`num'
} 

by cmcode: gen seq=_n
keep if seq==1
drop seq exp_g

foreach num of numlist 1(1)5{
gen exp_US_OR_`num' =(exp_g0_max==1 | exp_g`num'_max==1) 
gen exp_US_AND_`num'=(exp_g0_max==1 & exp_g`num'_max==1) 
} 

foreach num of numlist 1(1)5{
qui egen exp_US_OR_`num'_sum =sum(exp_US_OR_`num')
qui egen exp_US_AND_`num'_sum=sum(exp_US_AND_`num') 
qui gen exp_US_`num'_ratio=exp_US_AND_`num'_sum/exp_US_OR_`num'_sum
tab exp_US_`num'_ratio
}  
  
 